package com.touch.client.forms;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.swing.JOptionPane;
import com.touch.client.ClientControls;
import com.touch.client.ClientSocketEvents;
import com.touch.jgloo.owt.JGLButton;
import com.touch.jgloo.owt.JGLComponent;
import com.touch.jgloo.owt.JGLFrame;
import com.touch.jgloo.owt.JGLLabel;
import com.touch.jgloo.owt.JGLTextBox;
import com.touch.core.AccountConstants;
public class NewAccountGLEvents implements GLEventListener, MouseListener, KeyListener
{
	protected JGLButton bCreate;
	protected JGLButton bCancel;
	protected JGLTextBox tUsername;
	protected JGLTextBox tPassword;
	protected JGLLabel lUsername;
	protected JGLLabel lPassword;
	protected JGLLabel lMale, lFemale;
	protected JGLButton bMale, bFemale;
	public JGLFrame fNewAccount;
	
	private byte sex[] = {AccountConstants.MALE};
	public void display(GLAutoDrawable arg0) 
	{	
		fNewAccount.render();
	}
	public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) 		{}
	public void init(final GLAutoDrawable drawable) 
	{			
		fNewAccount = new JGLFrame("New Account", 320, 240);
		bCreate = new JGLButton("Create Account", 120, 25);	
		bCancel = new JGLButton("Cancel", 120, 25);
		tUsername = new JGLTextBox(120, "          ");
		tPassword = new JGLTextBox(120, "          ");
		lUsername = new JGLLabel("Username");
		lPassword = new JGLLabel("Password");
		lMale = new JGLLabel("Male");
		lFemale = new JGLLabel("Female");
		bMale = new JGLButton("X", 20, 20);
		bFemale = new JGLButton(" ", 20, 20);
		
		fNewAccount.add(bCreate);
		fNewAccount.add(bCancel);
		fNewAccount.add(tUsername);
		fNewAccount.add(tPassword);
		fNewAccount.add(lUsername);
		fNewAccount.add(lPassword);
		fNewAccount.add(lMale);
		fNewAccount.add(lFemale);
		fNewAccount.add(bMale);
		fNewAccount.add(bFemale);
		
		lUsername.setPosition(30, 60, 0);
		lPassword.setPosition(30, 90, 0);
		tUsername.setPosition(120, 60, 0);
		tPassword.setPosition(120, 90, 0);
		lMale.setPosition(45, 120, 0);
		bMale.setPosition(120, 120, 0);
		lFemale.setPosition(45, 140, 0);
		bFemale.setPosition(120, 140, 0);
		bCreate.setPosition(30, 200, 0);
		bCancel.setPosition(170, 200, 0);
	}
	public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) 	{}
	public void mouseClicked(MouseEvent e) 
	{	
		fNewAccount.mouseClicked(e);
		tUsername.mouseClicked(e);
		tPassword.mouseClicked(e);
		bCreate.mouseClicked(e);
		bCancel.mouseClicked(e);
		bMale.mouseClicked(e);
		bFemale.mouseClicked(e);
		
		if(JGLComponent.getSelected() == bCreate.getIndex())
		{		
			if(!ClientControls.CONNECTED)
				return;			
			String name = tUsername.getText().trim();
			String password = tPassword.getText().trim();
			
			if(name.length() < 3)
			{
				JOptionPane.showMessageDialog(null, "Account name must be at least 3 characters in length");
				return;
			}
			if(password.length() < 3)
			{
				JOptionPane.showMessageDialog(null, "Account password must be at least 3 characters in length");
				return;
			}
			
			byte[] message = ClientSocketEvents.NEW_ACCOUNT_PACKET(name.getBytes(), password.getBytes(), sex);			
			ClientControls.CLIENT_SOCKET.send(message);
		}
		else if(JGLComponent.getSelected() == bCancel.getIndex())
			fNewAccount.setVisible(false);
		else if(JGLComponent.getSelected() == bMale.getIndex())
		{
			bFemale.setCaption(" ");
			bMale.setCaption("X");
			sex[0] = AccountConstants.MALE;
		}
		else if(JGLComponent.getSelected() == bFemale.getIndex())
		{
			bMale.setCaption(" ");
			bFemale.setCaption("X");
			sex[0] = AccountConstants.FEMALE;
			//TODO: parse gender server-side
		}
	}
	public void mouseEntered(MouseEvent e) 		{}
	public void mouseExited(MouseEvent e)	 	{}
	public void mousePressed(MouseEvent e) 
	{	
		fNewAccount.mousePressed(e);		
	}
	public void mouseReleased(MouseEvent e) 
	{
		fNewAccount.mouseReleased(e);
	}
	public void keyPressed(KeyEvent e) 
	{	
		tUsername.keyPressed(e);
		tPassword.keyPressed(e);
	}
	public void keyReleased(KeyEvent e) 
	{	
		tUsername.keyReleased(e);
		tPassword.keyReleased(e);
	}
	public void keyTyped(KeyEvent e) 
	{	
		tUsername.keyTyped(e);
		tPassword.keyTyped(e);
	}
		
}
